﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Entity;
using JDEduTrain.Framework.Business;
using Wicresoft.Framework.Organization;

public partial class Page_AuditClassMemberDetail : BaseAuthenticationPage
{
    public Guid ClassID
    {
        get
        {
            if (Request["ClassID"] != null)
                return new Guid(Request["ClassID"]);
            return Guid.Empty;
        }
    }

    public string ApplyOrganID
    {
        get
        {
            if (ViewState["ApplyOrganID"] != null)
                return ViewState["ApplyOrganID"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["ApplyOrganID"] = value;
        }
    }

    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }

    protected string MainDeptID
    {
        get
        {
            if (ViewState["MainDeptID"] != null)
                return ViewState["MainDeptID"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["MainDeptID"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            BindClass();
            TrainClass trainClass = new TrainClass(ClassID);
            if (trainClass.Load())
            {
                ApplyOrganID = trainClass.ApplyOrganID;
            }
            if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.MinistryAdminRoleID))
            {
                tdAdd.Visible = false;
                tdAddFg.Visible = false;
            }
        }

    }





    protected void BindClass()
    {
        TrainClass trainClass = new TrainClass(ClassID);
        if (trainClass.Load())
        {
            tbBeginTime.Text = trainClass.BeginTime.ToShortDateString();
            tbClassName.Text = trainClass.ClassName;
            tbComment.Text = trainClass.Comment;
            tbEndTime.Text = trainClass.EndTime.ToShortDateString();
            tbCourseInfo.Text = trainClass.CourseInfo;
            tbScore.Text = trainClass.Score.ToString();
            tbPlace.Text = trainClass.Place;
            tbLectorInfo.Text = trainClass.LectorInfo;
            tbCurriculum.Text = trainClass.Curriculum;
            tbTrainLevel.Text = trainClass.TrainLevel;
        }
        string memberStatus = string.Empty;
        if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID))
        {
            switch (ddlMemberStatus.SelectedValue)
            {
                case "待审核":
                    memberStatus = "初审通过";
                    break;
                case "审核通过":
                    memberStatus = "终审通过";
                    break;
                case "审核驳回":
                    memberStatus = "终审驳回";
                    break;
                default:
                    memberStatus = "初审通过";
                    break;
            }

        }
        else
        {
            switch (ddlMemberStatus.SelectedValue)
            {
                case "待审核":
                    memberStatus = "待审核";
                    break;
                case "审核通过":
                    memberStatus = "初审通过";
                    break;
                case "审核驳回":
                    memberStatus = "初审驳回";
                    break;
                default:
                    memberStatus = "待审核";
                    break;
            }

            Department currMainDept = OrganizationHelper.GetDepartmentInfo(CurrentUser.MainDepartmentID);
            string deptPath = currMainDept.IDPath;
            string[] deptIDs = deptPath.Split('/');
            string mainDeptID = string.Empty;
            if (deptIDs.Length >= 4)
            {
                mainDeptID = deptIDs[3];
                MainDeptID = mainDeptID;
            }
        }
        if (!string.IsNullOrEmpty(MainDeptID))
        {
            //筛选该单位人事负责的学员
            DataTable dtMember = BLTrain.GetMemberByClassID(ClassID, memberStatus);
            DTUserPageList = dtMember.Clone();
            DTUserPageList.Clear();
            foreach (DataRow dr in dtMember.Rows)
            {
                if (OrganizationHelper.IsUserInDepartment(new Guid(dr["ID"].ToString()), new Guid(MainDeptID)))
                {
                    DTUserPageList.ImportRow(dr);
                }
            }
        }
        else
        {
            DTUserPageList = BLTrain.GetMemberByClassID(ClassID, memberStatus);
        }
        DTUserPageList.Columns.Add("OrderInfo");
        DTUserPageList.Columns.Add("ThirdLevelDeptDisplayName");
        foreach (DataRow dr in DTUserPageList.Rows)
        {
            Guid id = new Guid(dr["ID"].ToString());
            UserExtend userExtend = new UserExtend(id);
            if (userExtend.Load())
            {
                dr["ThirdLevelDeptDisplayName"] = userExtend.ThirdLevelDeptDisplayName;
            }
            try
            {
                ClassOrder order = new ClassOrder(new Guid(dr["OrderID"].ToString()), new Guid(dr["ClassID"].ToString()));
                if (order.Load())
                {
                    dr["OrderInfo"] = order.BeginTime.ToShortDateString() + "至" + order.EndTime.ToShortDateString() + ";" + order.BeginHour + "至" + order.EndHour + ";";
                }
            }
            catch (Exception)
            {
            }
        }
        rptMemberInfo.DataSource = DTUserPageList;
        rptMemberInfo.DataBind();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindClass();
    }

    protected void btnAuditPass_Click(object sender, EventArgs e)
    {
        ArrayList alClass = new ArrayList();

        for (int i = 0; i < rptMemberInfo.Items.Count; i++)
        {
            if ((rptMemberInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptMemberInfo.Items[i].ItemIndex]["ID"].ToString());
                alClass.Add(id);
            }
        }
        BLTrain.AuditPassClassMember(Wicresoft.Common.Configuration.ConfigurationSettings.AppSettings["JDEdutrain.MoudleID"], CurrentUser.ID, (Guid[])alClass.ToArray(typeof(Guid)), ClassID, OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID));
        BindClass();

    }
    protected void btnAuditFail_Click(object sender, EventArgs e)
    {
        ArrayList alClass = new ArrayList();

        for (int i = 0; i < rptMemberInfo.Items.Count; i++)
        {
            if ((rptMemberInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptMemberInfo.Items[i].ItemIndex]["ID"].ToString());
                alClass.Add(id);
            }
        }
        BLTrain.AuditFailClassMember((Guid[])alClass.ToArray(typeof(Guid)), ClassID, OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID), tbAuditFailReason.Text);
        BindClass();
        tbAuditFailReason.Text = string.Empty;
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ArrayList alUser = new ArrayList();

        for (int i = 0; i < rptMemberInfo.Items.Count; i++)
        {
            if ((rptMemberInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptMemberInfo.Items[i].ItemIndex]["ID"].ToString());
                alUser.Add(id);
            }
        }
        BLTrain.DeleteClassMember((Guid[])alUser.ToArray(typeof(Guid)), ClassID);
        BindClass();
    }



    protected void ddlMemberStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
    }

    protected void rptUserInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lFullName = e.Item.FindControl("lFullName") as Label;

            string id = DTUserPageList.Rows[e.Item.ItemIndex]["ID"].ToString();
            NameControlLib.NameInfo userControl = new NameControlLib.NameInfo();
            userControl.ID = "UserNameControl_" + id;
            userControl.UserID = new Guid(id);
            userControl.ScriptPath = "../Js/NameInfo.js";
            lFullName.Controls.Add(userControl);
        }
    }


}
